package com.iweb.day06.factorydemo;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.Scanner;

/**
 * @author WuYongheng
 * @date 2021/11/4
 * @description 测试工厂模式的客户端
 */
public class Client {

    /**
     * 获取配置文件中保存的类的全限定名
     *
     * @return
     */
    public static String fectchClassName(String name) {
        try {
            Properties p = new Properties();
            InputStream inputStream = Client.class.getClassLoader().getResourceAsStream("tastetype.properties");
            p.load(inputStream);
            String content = p.getProperty(name);
            return content;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) throws ClassNotFoundException,
            InstantiationException, IllegalAccessException {

        Scanner scanner = new Scanner(System.in);

        // 输入想要的口味
        System.out.println("请输入想要的口味：（Strawberry,Blueberry,Banana）");
        String name = scanner.nextLine();

        // 1、获取配置文件中保存的类的全限定名
        String className = Client.fectchClassName(name);
        if (className == null || className.equals("")) {
            System.out.println("暂时未获取到客户端需求");
        }

        // 2、通过反射，根据类的全限定名，找到对应的.class文件
        // 然后加载到jvm，实例化对象
        IceCreamFactory iceCreamFactory =
                (IceCreamFactory) (Class.forName(className).newInstance());

//       IceCreamFactory iceCreamFactory = null;
        //此处有耦合，使用配置文件加反射优化掉耦合
//        switch (tasteType) {
//            case "草莓":
//                iceCreamFactory = new StrawberryFactory();
//                break;
//            case "蓝莓":
//                iceCreamFactory = new BlueberryFactory();
//                break;
//            case "香蕉":
//                iceCreamFactory = new BananaFactory();
//                break;
//            default:
//                System.out.println(tasteType + "口味正在计划筹备中!请谅解");
//                break;
//        }

        // 有了工厂后，就可以创建产品了
        Taste taste = iceCreamFactory.createTaste();
        // 进行业务操作
        taste.make();
    }
}
